package com.example.receiptocr.home

import android.content.Intent
import android.os.Bundle
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import java.math.BigDecimal
import androidx.compose.runtime.getValue
import androidx.compose.ui.platform.LocalContext
import androidx.hilt.navigation.compose.hiltViewModel
import com.example.receiptocr.ocr.OCRResultActivity
import com.example.receiptocr.viewModels.ExpenseChartState
import com.example.receiptocr.viewModels.ReceiptViewModel
import com.example.receiptocr.viewModels.ReceiptsState
import java.io.Serializable

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun HomeScreen(
    navController: NavController,
    viewModel: ReceiptViewModel = hiltViewModel(),
    onNavigateToCamera: () -> Unit = {}
) {
    val context = LocalContext.current
    val receiptsState by viewModel.receiptsState
    val expenseChartState by viewModel.expenseChartState

    LaunchedEffect(Unit) {
        viewModel.loadReceipts()
        viewModel.loadWeeklyExpenses()
    }

    Column(
        modifier = Modifier
            .fillMaxSize()
            .verticalScroll(rememberScrollState())
            .padding(16.dp)
    ) {
        Button(
            onClick = onNavigateToCamera,
            modifier = Modifier
                .fillMaxWidth()
                .padding(bottom = 24.dp)
        ) {
            Text(text = "📷 拍照上传票据")
        }

        // 说明卡片
        Card(
            modifier = Modifier
                .fillMaxWidth()
                .padding(bottom = 16.dp),
            colors = CardDefaults.cardColors(
                containerColor = MaterialTheme.colorScheme.surfaceVariant
            )
        ) {
        }

        Text(
            text = "最近识别的票据",
            fontSize = 18.sp,
            fontWeight = FontWeight.SemiBold,
            modifier = Modifier.padding(bottom = 12.dp)
        )

        when (receiptsState) {
            is ReceiptsState.Loading -> {
                CircularProgressIndicator(
                    modifier = Modifier.align(Alignment.CenterHorizontally)
                )
            }
            is ReceiptsState.Empty -> {
                Text(
                    text = "暂无票据记录",
                    color = Color.Gray,
                    modifier = Modifier.padding(vertical = 16.dp)
                )
            }
            is ReceiptsState.Success -> {
                val receipts = (receiptsState as ReceiptsState.Success).receipts
                receipts.forEach { receipt ->
                    ReceiptCard(
                        receipt = receipt,
                        onClick = {
                            // 导航到票据详情页面
                            val intent = Intent(context, DetailActivity::class.java).apply {
                                // 如果需要传递图片路径
                                putExtra("receiptInfo", receipt)
                                // 添加 FLAG_ACTIVITY_NEW_TASK 标志
                                addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                            }
                            context.startActivity(intent)
                        }
                    )
                }
            }
            is ReceiptsState.Error -> {
                Text(
                    text = (receiptsState as ReceiptsState.Error).message,
                    color = Color.Red,
                    modifier = Modifier.padding(vertical = 8.dp)
                )
            }
        }
        Spacer(modifier = Modifier.height(24.dp))
        
        Text(
            text = "近7天消费趋势",
            fontSize = 18.sp,
            fontWeight = FontWeight.SemiBold,
            modifier = Modifier.padding(bottom = 12.dp)
        )
        
        when (expenseChartState) {
            is ExpenseChartState.Loading -> {
                CircularProgressIndicator(
                    modifier = Modifier.align(Alignment.CenterHorizontally)
                )
            }
            is ExpenseChartState.Empty -> {
                Text(
                    text = "暂无消费数据",
                    color = Color.Gray,
                    modifier = Modifier.padding(vertical = 16.dp)
                )
            }
            is ExpenseChartState.Success -> {
                ExpenseChart(
                    state = expenseChartState,
                    modifier = Modifier.fillMaxWidth()
                )
            }
            is ExpenseChartState.Error -> {
                Column {
                    Text(
                        text = "加载失败",
                        color = Color.Red,
                        modifier = Modifier.padding(vertical = 8.dp)
                    )
                    Button(
                        onClick = { viewModel.loadWeeklyExpenses() },
                        modifier = Modifier.align(Alignment.CenterHorizontally)
                    ) {
                        Text("重试")
                    }
                }
            }
        }
    }
}

@Preview(showBackground = true)
@Composable
fun PreviewHomeScreen() {
    HomeScreen(
        navController = rememberNavController(),
    )
}
